package com.chii.colorization.ui.fragment.MainActivity.tools;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.chii.colorization.R;
import com.chii.colorization.ui.activity.LoginActivity;
import com.chii.colorization.ui.activity.MainActivity;
import com.chii.colorization.ui.activity.PayActivity;
import com.chii.colorization.utils.connes.MyUrl;
import com.chii.colorization.utils.connes.OkHttpUtil;

import java.io.IOException;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.chii.colorization.utils.ActivityCodes.REQUESTCODE_FROM_LOGIN;

public class ToolsFragment extends Fragment {
    @BindView(R.id.check)
    Button check;
    @BindView(R.id.text_tools)
    TextView textTools;

    private Unbinder unbinder;
    private Handler mHandler;

    private ToolsViewModel toolsViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        toolsViewModel = ViewModelProviders.of(this).get(ToolsViewModel.class);
        View root = inflater.inflate(R.layout.fragment_tools, container, false);
        unbinder = ButterKnife.bind(this, root);
        toolsViewModel.getText().observe(this, new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textTools.setText(s);
            }
        });
        initHandler();
        return root;
    }

    private void initHandler() {
        mHandler = new Handler(new Handler.Callback() {
            @Override
            public boolean handleMessage(Message msg) {
                switch (msg.what) {
                    case 1:
                        textTools.setText((String) msg.obj);
                        break;
                    default:
                }
                return false;
            }
        });
    }

    @Override
    public void onDestroyView() {
        unbinder.unbind();
        super.onDestroyView();
    }

    @OnClick(R.id.check)
    public void onViewClicked() {

        OkHttpUtil okHttpUtil = OkHttpUtil.getInstance(getActivity());
        okHttpUtil.get(MyUrl.isRemembermeUser, null, new OkHttpUtil.MyCallback() {
            @Override
            public void success(String s) {
                System.out.println(s);
                Message message = new Message();
                message.what = 1;
                message.obj = s;
                mHandler.sendMessage(message);
            }

            @Override
            public void failed(IOException e) {
            }
        });
    }

}